package main

import (
	"fmt"
	"sync"
	"time"
)
var group int
func job1() {
	fmt.Println("job1正在执行")
for group=0;group<5;group++{
	fmt.Println("唤醒分组",group)
	cond.Broadcast()
	time.Sleep(time.Second )
}

}
func job2() {
	for group!=2{
		cond.L.Lock()
		cond.Wait()
		cond.L.Unlock()
	}
	fmt.Println("job2正在执行")
	time.Sleep(time.Second * 1)
}
func job3() {
	for group!=3{
		cond.L.Lock()
		cond.Wait()
		cond.L.Unlock()
	}
	fmt.Println("job3正在执行")
}
func do(fs ...func()) *sync.WaitGroup {
	mg := &sync.WaitGroup{}
	for _, f := range fs {
		mg.Add(1)
		go func(f func()) {
			defer mg.Done()
			f()
		}(f)
	}
	return mg
}

var cond *sync.Cond

func main() {
	cond = sync.NewCond(&sync.Mutex{})
	mg := do(job1, job2, job3)
	mg.Wait()
}
